package sky.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sky.common.BaseContext;
import sky.common.R;
import sky.pojo.Orders;
import sky.service.OrderDetailService;
import sky.service.OrdersService;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/order")
@Slf4j
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;




    /**
     * 页面显示——输入框查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime){
        log.info("beginTime:{}",beginTime);
        log.info("endTime:{}",endTime);
        //页面构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        //查询所有orders表信息
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //查询name
        if (number != null){
            queryWrapper.like(Orders::getNumber, number);
        }
        //查询beginTime 大于等于这个时间
        if (beginTime != null){
            queryWrapper.ge(Orders::getOrderTime, beginTime);
        }
        //查询endTime 小于等于这个时间
        if (endTime != null){
            queryWrapper.le(Orders::getOrderTime, endTime);
        }
        queryWrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }


    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        log.info("page:{},pageSize:{}",page,pageSize);

        Long userId = BaseContext.getCurrentId();
        log.info("用户Id:{}",userId);

        Page<Orders> orderPage=new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        //查询当前用户的订单信息
        lambdaQueryWrapper.eq(Orders::getUserId,userId);
        lambdaQueryWrapper.orderByAsc(Orders::getCheckoutTime);

        ordersService.page(orderPage);

        return R.success(orderPage);
    }

}
